package com.erp.controller;

import com.erp.mapper.LogMapper;
import com.erp.mapper.UserMapper;
import com.erp.pojo.Log;
import com.erp.pojo.User;
import com.erp.tool.PgTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class UserController {

    @Autowired
    private UserMapper mapper;
    @Autowired
    private LogMapper logMapper;


    //登录
    @RequestMapping("login")
    public Map login(User user, HttpSession session,HttpServletRequest request){
        Map<String,Object> user1 = mapper.userLogin(user);
        if (user1!=null){
            session.setAttribute("fId",user1.get("f_id"));
            session.setAttribute("account",user1.get("u_account"));
            session.setAttribute("name",user1.get("u_name"));
            session.setAttribute("id",user1.get("u_id"));
            System.out.println(user1);
            int i= (int) user1.get("state");
            if (i==1){
                user1.put("info","账号已在线,请稍后再尝试！");
                user1.put("url","login");
            }else{
                //该变用户状态
                mapper.updateStateLogin((String) user1.get("u_account"));
                System.out.println("用户名："+request.getSession().getAttribute("name")+"登录了");
                logMapper.insert(new Log(0,"用户名："+request.getSession().getAttribute("name")+"登录了",2,new Date(),Integer.parseInt(String.valueOf(request.getSession().getAttribute("id")))));
                user1.put("info","登录成功！");
                user1.put("url","index");
            }
        }
        return user1;
    }

    //退出
    //@LoginRequired
    @RequestMapping("/logout")
    public Map logout(HttpServletRequest request){
        Map<String,String> map=new HashMap<String,String>();
        //改变用户状态
        try {
            mapper.updateStateLogout((String)request.getSession().getAttribute("account"));
            System.out.println("用户："+request.getSession().getAttribute("name")+"退出登录");
            logMapper.insert(new Log(0,"用户名："+request.getSession().getAttribute("name")+"退出登录",2, new Date(),Integer.parseInt(String.valueOf(request.getSession().getAttribute("id")))));
            //删除会话
            request.getSession().removeAttribute("account");
            request.getSession().removeAttribute("fId");
            request.getSession().removeAttribute("name");
        }catch (NullPointerException e){
            logMapper.insert(new Log(0,"报错！"+e.getMessage(),1,new Date(),Integer.parseInt(String.valueOf(request.getSession().getAttribute("id")))));
            e.printStackTrace();
        }
        //回到登录页面
        map.put("url","login");
        return map;
    }

    //查询用户名是否重名
    @RequestMapping("/changeName")
    public Map changeName(String name){
        Map<String,String> map=new HashMap<String,String>();
        int countId = mapper.findNameCountId(name);
        if (countId!=1){
            map.put("exist","0");
        }else {
            map.put("exist","1");
        }
        return map;
    }

    //注册
    @RequestMapping("/registered")
    public Map registered(User user,HttpServletRequest request){
        Map<String,String> map=new HashMap<String,String>();
        try {
            //初始化账号
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
            String format = simpleDateFormat.format(date)+"00";
            String s = mapper.findAccountLimit().substring(6);
            int i=Integer.parseInt(s)+1;
            map.put("account",format+i);
            //账号
            user.setUAccount(format+i);
            //账号存在
            user.setFlag(1);
            //注册都是普通用户
            user.setAId(4);
            //默认头像
            user.setUImg("img/a3.jpg");
            //当前时间
            user.setCreateDate(date);
            int addUser = mapper.addUser(user);
            if (addUser==1){
                map.put("info","欢迎加入我们！"+user.getUName());
            }
        }catch (NullPointerException e){
            System.out.println("功能id截取错误");
            logMapper.insert(new Log(0,e.getMessage(),1,new Date(),Integer.parseInt(String.valueOf(request.getSession().getAttribute("id")))));
        }
        return map;
    }

    //分页查
    //@LoginRequired
    @RequestMapping("/limitUsers")
    public List limitUsers(User user,int num){
        List<Object>allList = new ArrayList<Object>();
        int row=4;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("name",user.getUName());
        map.put("gender",user.getUGender());
        map.put("account",user.getUAccount());
        map.put("end",row);
        map.put("start", PgTool.getStart(num,row).get("start"));
        System.out.println(map);
        //查询需多少页
        allList.add(PgTool.getPage(row, mapper.countUsers(map)));
        //得到分页数据
        allList.add(mapper.findAllUsers(map));
        System.out.println(allList);
        return allList;
    }



    //软删除
    //@LoginRequired
    @RequestMapping("/remove")
    public Map removeUser(int id){
        Map<String,String> map=new HashMap<String,String>();
        map.put("state","fail");
        if (mapper.removeUser(id)>0){
            map.put("state","success");
        }
        return  map;
    }

    //修改
    //@LoginRequired
    @RequestMapping("/updateUser")
    public Map updateUser(User user){
        Map<String,String> map=new HashMap<String,String>();
        map.put("state","fail");
        if (mapper.updateUser(user)>0){
            map.put("state","success");
        }
        return  map;
    }

    //查询session做拦截
    @RequestMapping("/common")
    public Map common(HttpServletRequest request){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("state",false);
        map.put("url","login.html");
        //说明为有登陆
        if (request.getSession().getAttribute("name")!=null){
                map.put("state",true);
        }
        return map;
    }

    @RequestMapping("/findOnlineUsers")
    public Map findOnlineUsers(){
        Map<String,Object> map=new HashMap<String,Object>();
        List<User> onlineUsers = mapper.findOnlineUsers();
        map.put("online",onlineUsers.size());
        map.put("noOnline",mapper.countUsers(map)-onlineUsers.size());
        map.put("onlineData",onlineUsers);
        return  map;
    }

    //分页查在线用户
    @RequestMapping("/limitOnlineUsers")
    public List limitOnlineUsers(User user,int num){
        List<Object>allList = new ArrayList<Object>();
        int row=3;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("name",user.getUName());
        map.put("end",row);
        map.put("start", PgTool.getStart(num,row).get("start"));
        //查询需多少页
        allList.add(PgTool.getPage(row, mapper.countOnlineUsers(map)));
        //得到分页数据
        allList.add(mapper.limitOnlineUsers(map));
        return allList;
    }

    //清楚用户登录状态
    @RequestMapping("/updateLogout")
    public Map logoutOnlineUser(int uId){
        Map<String,String> map=new HashMap<String,String>();
        map.put("state","fail");
        if (mapper.logoutOnlineUser(uId)>0){
            map.put("state","success");
        }
        return  map;
    }
}
